const swaggerJSDoc = require('swagger-jsdoc');
const { koaSwagger } = require('koa2-swagger-ui');
const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: '菜奔奔后台管理系统 API',
      version: '1.0.0',
      description: 'API 文档',
    },
    servers: [
      { url: 'http://localhost:1234', description: '本地开发环境' },
    ],
  },
  apis: ['./routes/*.js'],
};

const swaggerSpec = swaggerJSDoc(options);

module.exports = (router) => {
  router.post('/swagger.json', async (ctx) => {
    ctx.body = swaggerSpec;
  });

  router.post('/docs', koaSwagger({
    routePrefix: false,
    swaggerOptions: {
      url: '/swagger.json',
    },
  }));
};
